package com.appbee.cm.client.nav.presenter;

import javax.inject.Inject;

import com.appbee.cm.client.regionunit.presenter.HasActivityEntry;
import com.appbee.cm.shared.data.ActivityEntryProxy;
import com.appbee.core.client.json.Json;
import com.appbee.core.client.place.HasPlace;
import com.appbee.core.client.place.INamePlace;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class SimpleNavActivity extends AbstractActivity implements
		HasActivityEntry, HasPlace
{
	private final SimpleNavPresenter.View view;
	private ActivityEntryProxy activityEntry;
	private final SimpleNavPresenter navPresenter;
	private final NavStyleRegistry navStyleRegistry;
	private Place place;
	private final NavControl navControl;

	@Inject
	public SimpleNavActivity(	SimpleNavPresenter.View view,
								SimpleNavPresenter navPresenter,
								NavControl navControl,
								NavStyleRegistry navStyleRegistry)
	{
		this.view = view;
		this.navPresenter = navPresenter;
		this.navControl = navControl;
		this.navStyleRegistry = navStyleRegistry;

	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus)
	{
		if (place instanceof INamePlace)
		{
			INamePlace namePlace = (INamePlace) place;
			NavItemWrap navItem = navControl.getByPlace(namePlace);
			view.setSelectedItem(navItem);
		}
		Json settings = Json.parseStrict(activityEntry.getSettings());
		view.setNavStyle(navStyleRegistry.getById(settings
				.getString("style", null)).getStyle());
		navPresenter.setParentId(settings.getStringAsLong("parentId", null));
		navPresenter.setView(view);
		panel.setWidget(view);
	}

	@Override
	public void setActivityEntry(ActivityEntryProxy activityEntry)
	{
		this.activityEntry = activityEntry;
	}

	@Override
	public void setPlace(Place place)
	{
		this.place = place;
	}

}
